package com.tentact.coolmeetingtest.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * @author 星环
 * @date 2023/1/10 15:52:24
 * @description
 */
public class StringUtil {

    public static boolean isNullOrEmptyStr(String str){
        return str == null || "".equals(str);
    }

    /**
     * 若不是空、null字符串，就返回Integer.parseInt否则返回-1
     * @param str
     * @return
     */
    public static int parseIntWithNullEmptyCheck(String str){
        if(!isNullOrEmptyStr(str)){
            return Integer.parseInt(str);
        }
        return -1;
    }

    public static String beanToSQLName(String name){
        StringBuilder sb = new StringBuilder(name.length()*2);
        sb.append(name);
        for(int i = 0;i < sb.length();i++){
            char ch = sb.charAt(i);
            if(Character.isUpperCase(ch)){
                sb.deleteCharAt(i);
                sb.insert(i,Character.toLowerCase(ch));
                if(i != 0){
                    sb.insert(i,'_');
                }
            }
        }
        return sb.toString();
    }

    public static String setterToSQLName(String name){
        String s = name.substring(3);
        return beanToSQLName(s);
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private static final StringBuilder sb = new StringBuilder();

    public static Timestamp webTimeToTimestamp(String string){
        //string.replaceAll("-")
        if(string == null){
            return null;
        }
        synchronized (sb){
            string = sb.append(string).append(":00").toString();
            sb.setLength(0);
        }
        System.out.println("[log] string to be convert to Timestamp is " + string);
        return Timestamp.valueOf(string);

    }


}
